import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Dimension;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;



/**
 * Main GUI class of Courier application
 * 
 * @author Chia-Chi (Teresa) Lee
 */

public class CourierMain extends JFrame {
  
  private int INIT_WINDOW_WIDTH = 1097;
  private int INIT_WINDOW_HEIGHT =  768;
  private JLabel status;
  
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        CourierMain c = new CourierMain();
        c.initGUI();
        c.setVisible(true);
      }
    });
  }
  
  /**
   * For creating the initial interface of the Courier app
   * 
   */ 
  public void initGUI() {
    
    //begin initiating left side
    JTabbedPane left = new JTabbedPane(JTabbedPane.TOP);
    left.setMinimumSize(new Dimension(230, 0));
    Browser browser = new Browser();
    browser.init();
    AddressBook address = new AddressBook();
    address.init();
    left.addTab("Web Broswer", browser);
    left.addTab("Address Book",address);
    
    //begin initiating right side
    //create right-side content
    final Notebook notebook = new Notebook();
    notebook.init();
    JScrollPane rightContent = new JScrollPane(notebook); 
    //create right-side control
    JPanel control = new JPanel();
    control.setLayout(new FlowLayout());
    
    String[] fontFamilies = { "Dialog", "DialogInput", "Monospaced", "Serif", "SansSerif" };
    JComboBox fontList = new JComboBox(fontFamilies);
    fontList.setSelectedIndex(0);
    fontList.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        String font = (String)cb.getSelectedItem();
        notebook.fontFamily = font;
      }
    });
    
    final JButton backward = new JButton("<");
    final JButton forward = new JButton(">");
    backward.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              notebook.prevPage();
              backward.setEnabled(notebook.isBackEnabled());
              forward.setEnabled(notebook.isForEnabled());
         }
       });
    backward.setEnabled(false);
    forward.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              notebook.nextPage();
              backward.setEnabled(notebook.isBackEnabled());
              forward.setEnabled(notebook.isForEnabled());
         }
       });
    forward.setEnabled(false);
    JButton newPage = new JButton("New");
    newPage.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              notebook.newPage();
              backward.setEnabled(notebook.isBackEnabled());
              forward.setEnabled(notebook.isForEnabled());
         }
       });
    JButton deletePage = new JButton("Delete");
    deletePage.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              notebook.deletePage();
              backward.setEnabled(notebook.isBackEnabled());
              forward.setEnabled(notebook.isForEnabled());
         }
       });
    JRadioButton free = new JRadioButton("Free-Form Ink");
    JRadioButton rectangle = new JRadioButton("Rectangle");
    JRadioButton oval = new JRadioButton("Oval");
    ChangeListener changeListener = new ChangeListener() {
      public void stateChanged(ChangeEvent changEvent) {
        AbstractButton aButton = (AbstractButton)changEvent.getSource();
        ButtonModel aModel = aButton.getModel();
        if(aModel.isSelected()){
        notebook.mode = aButton.getText();
        }
      }
    };
    free.addChangeListener(changeListener);
    rectangle.addChangeListener(changeListener);
    oval.addChangeListener(changeListener);
    ButtonGroup drawControl = new ButtonGroup();
    drawControl.add(free);
    drawControl.add(rectangle);
    drawControl.add(oval);
    control.add(newPage);
    control.add(deletePage);
    control.add(backward);
    control.add(forward);
    control.add(fontList);
    control.add(free);
    control.add(rectangle);
    control.add(oval);
    //create right-side panel
    JPanel right = new JPanel();
    right.setLayout(new BorderLayout());
    right.setMinimumSize(new Dimension(650, 0));
    right.add(rightContent,BorderLayout.CENTER);
    right.add(control,BorderLayout.SOUTH);
    
    //add right and left side to a JSplitPane
    JSplitPane content = new JSplitPane();
    content.setLeftComponent(left);
    content.setRightComponent(right);
    
    //begin initiating status bar
    status = new JLabel("Status Bar");
    
    //initiate window layout
    JPanel window = new JPanel();
    window.setLayout(new BorderLayout());
    window.add(content,BorderLayout.CENTER);
    window.add(status,BorderLayout.SOUTH);
    
    //initiate JFrame properties
    setTitle("Courier");
    setSize(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setMinimumSize(new Dimension(950, 400));
    add(window);
  }
}